package com.superhelper.task.job.stat;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.stat.StatDailyDeliver;
import com.superhelper.common.mapper.stat.StatDailyDeliverMapper;

public class StatDailyDeliverJob extends AbstractDailyJob implements SimpleJob {
    private final static Logger LOG = LoggerFactory.getLogger(StatDailyDeliverJob.class);

    @Autowired
    private StatDailyDeliverMapper statDailyDeliverMapper;

    @Override
    public void execute(ShardingContext shardingContext) {
        // {"aUserShopId":1,"startDate":"2018-1-1","endDate":"2018-1-2"}

        // String jobParam = shardingContext != null ?
        // shardingContext.getJobParameter()
        // : "{\"startDate\":\"2017-08-12\"}";
        String jobParam = shardingContext.getJobParameter();
        DateInfo di = calJobDate(jobParam, -1);
        LOG.info("startDate:{},endDate:{},statDate:{}", di.startDate, di.endDate, di.statDate);

        // 开始订单统计
        List<StatDailyDeliver> stats = statDailyDeliverMapper.statDailyDeliverDist(di.startDate, di.endDate);
        if (CollectionUtils.isEmpty(stats)) {
            LOG.info("end...jobParam:{},size:0", jobParam);
            return;
        }
        // 开始快递单统计
        List<StatDailyDeliver> statDelivers = statDailyDeliverMapper.statDailyDeliver(di.startDate, di.endDate);
        for (StatDailyDeliver o : stats) {
            AuserShop shop = shopCache.getShop(o.getShopId(), o.getShopType());
            if (shop == null) {
                continue;
            }
            o.setAuserId(shop.getAuserId().longValue());
            o.setaUserShopId(shop.getId());
            o.setStatDate(di.statDate);

            for (int i = 0; i < statDelivers.size(); i++) {
                StatDailyDeliver statD = statDelivers.get(i);
                if (statD.getaUserShopId().longValue() == o.getaUserShopId().longValue()) {
                    o.setDeliverAmount(statD.getDeliverAmount());
                    o.setTipAmount(statD.getTipAmount());
                    o.setDeliverCnt(statD.getDeliverCnt());
                    o.setAvgAmount(o.getDeliverAmount() / (o.getDeliverCnt() + 0.0000001));
                    o.setAvgCost(statD.getAvgCost());
                    statDelivers.remove(i);
                    break;
                }
            }
            o.setDeliverAmount(o.getDeliverAmount() == null ? 0 : o.getDeliverAmount());
            o.setTipAmount(o.getTipAmount() == null ? 0 : o.getTipAmount());
            o.setDeliverCnt(o.getDeliverCnt() == null ? 0 : o.getDeliverCnt());
            o.setAvgAmount(o.getAvgAmount() == null ? 0 : o.getAvgAmount());
            o.setAvgCost(o.getAvgCost() == null ? 0 : o.getAvgCost());

        }
        if (CollectionUtils.isNotEmpty(stats)) {
            statDailyDeliverMapper.batchAdd(stats);
        }
        LOG.info("end...jobParam:{},size:{}", jobParam, stats.size());
    }
}
